package com.example.accounting.slice;

import com.example.accounting.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.Arrays;

public class AddAccountSlice extends AbilitySlice {
    private Text data_date_field;
    private TextField info_field;
    private TextField money_field;
    private TextField account_field;
    private Text type_text;
    private TableLayout select_project;
    private Image[] images;
    private Button[] buttons;
    private int selectButtonIndex = 0;
    private String selectButtonVal;
    private Button button_confirm_add;
    private static final ArrayList<String> buttonTextOutcome = new ArrayList<>(
            Arrays.asList("餐饮", "日用", "交通", "娱乐", "旅行", "购物", "美容", "学习", "办公", "宠物", "通讯", "住房", "医疗", "健身", "美妆", "服饰", "缴费", "教育", "数码")
    );
    private static final ArrayList<String> buttonTextIncome = new ArrayList<>(
            Arrays.asList("工资", "兼职", "理财", "礼金", "股市", "金属", "转账", "现金", "红包", "报销", "打赏", "礼物", "打赏", "奖金", "出售", "其他")
    );
    private static final int DIALOG_WIDTH = 900;

    private static final int DIALOG_HEIGHT = 900;

    private static final int DIALOG_CORNER_RADIUS = 30;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_add_account);
        System.out.println("enter add account");

        Button button_outcome = (Button) findComponentById(ResourceTable.Id_select_outcome);
        Button button_income = (Button) findComponentById(ResourceTable.Id_select_income);

        type_text = (Text) findComponentById(ResourceTable.Id_data_type);
        button_confirm_add = (Button) findComponentById(ResourceTable.Id_confirm_add);

        initTextField();

        Element element = button_confirm_add.getBackgroundElement();
        selectButtonVal = "支出";
        button_outcome.setBackground(element);
        button_income.setBackground(null);

        initOutcome();

        type_text.setText("请选择类别");

        button_outcome.setClickedListener(component -> {
            button_outcome.setBackground(element);
            button_income.setBackground(null);
            if (selectButtonVal.equals("收入")) {
                type_text.setText("请选择类别");
                selectButtonVal = "支出";
                initOutcome();
            }
        });

        button_income.setClickedListener(component -> {
            type_text.setText("请选择类别");
            button_income.setBackground(element);
            button_outcome.setBackground(null);
            if (selectButtonVal.equals("支出")) {
                selectButtonVal = "收入";
                initIncome();
            }
        });


        button_confirm_add.setClickedListener(component -> {
            String date = data_date_field.getText();
            String money = money_field.getText();
            String type = type_text.getText();
            String info = info_field.getText();
            String account = account_field.getText();

            // 判空
            if (type.equals("请选择类别")) {
                makeToast("请选择类别");
            } else if (date.equals("")) {
                makeToast("请输入日期");
            } else if (info.equals("")) {
                makeToast("请输入详情");
            } else if (money.equals("")) {
                makeToast("请输入金额");
            } else {
                double money_val = 0;
                try {
                    money_val = Double.parseDouble(money);
                    if (selectButtonVal.equals("支出")) {
                        money_val = -money_val;
                    }
                    Intent intent1 = new Intent();
                    intent1.setParam("intent", "insert");
                    intent1.setParam("date", date);
                    intent1.setParam("money", money_val);
                    intent1.setParam("type", type);
                    intent1.setParam("info", info);
                    intent1.setParam("account", account);

                    present(new MainAbilitySlice(), intent1);
                } catch (Exception e) {
                    makeToast("请输入正确形式的金额");
                }
            }
        });
    }

    private void showContactDetailDialog() {
        Component container = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_select_year_month_day, null, false);

        DatePicker datePicker = (DatePicker) container.findComponentById(ResourceTable.Id_data_picker_ymd);
        Button button_yes = (Button) container.findComponentById(ResourceTable.Id_select_yes_ymd);
        Button button_no = (Button) container.findComponentById(ResourceTable.Id_select_no_ymd);


        CommonDialog commonDialog = new CommonDialog(this);
        commonDialog.setSize(DIALOG_WIDTH, DIALOG_HEIGHT);
        commonDialog.setCornerRadius(DIALOG_CORNER_RADIUS);
        commonDialog.setContentCustomComponent(container);
        commonDialog.show();
        button_yes.setClickedListener(component -> {
            data_date_field.setText(datePicker.getYear() + "年" + datePicker.getMonth() + "月" + datePicker.getDayOfMonth() + "日");
            commonDialog.hide();
        });

        button_no.setClickedListener(component -> commonDialog.hide());
    }

    private void initIncome() {

        select_project = (TableLayout) findComponentById(ResourceTable.Id_select_project);
        select_project.removeAllComponents();
        select_project.setColumnCount(4);
        select_project.setRowCount(4);
        buttons = new Button[16];
        images = new Image[16];

        for (int i = 0; i < 16; i++) {
            DirectionalLayout directionalLayout = new DirectionalLayout(this);
            directionalLayout.setAlignment(LayoutAlignment.CENTER);
            directionalLayout.setMarginsLeftAndRight(60, 60);
            directionalLayout.setMarginsTopAndBottom(50, 50);

            images[i] = new Image(this);
            images[i].setPixelMap(ResourceTable.Media_icon);
            images[i].setAlpha((float) 0.2);

            buttons[i] = new Button(this);
            buttons[i].setText(buttonTextIncome.get(i));
            buttons[i].setTextSize(80);
            buttons[i].setAlpha((float) 0.2);

            directionalLayout.addComponent(images[i]);
            directionalLayout.addComponent(buttons[i]);

            int finalI = i;
            directionalLayout.setClickedListener(component -> {
                type_text.setText(buttons[finalI].getText());
                images[selectButtonIndex].setAlpha((float) 0.2);
                buttons[selectButtonIndex].setAlpha((float) 0.2);
                images[finalI].setAlpha(1);
                buttons[finalI].setAlpha(1);
                selectButtonIndex = finalI;
            });

            select_project.addComponent(directionalLayout);
        }

    }

    private void initOutcome() {
        select_project = (TableLayout) findComponentById(ResourceTable.Id_select_project);
        select_project.removeAllComponents();
        select_project.setColumnCount(4);
        select_project.setRowCount(4);
        buttons = new Button[16];
        images = new Image[16];

        for (int i = 0; i < 16; i++) {
            DirectionalLayout directionalLayout = new DirectionalLayout(this);
            directionalLayout.setAlignment(LayoutAlignment.CENTER);
            directionalLayout.setMarginsLeftAndRight(60, 60);
            directionalLayout.setMarginsTopAndBottom(50, 50);

            images[i] = new Image(this);
            images[i].setPixelMap(ResourceTable.Media_icon);
            images[i].setAlpha((float) 0.2);

            buttons[i] = new Button(this);
            buttons[i].setText(buttonTextOutcome.get(i));
            buttons[i].setTextSize(80);
            buttons[i].setAlpha((float) 0.2);

            directionalLayout.addComponent(images[i]);
            directionalLayout.addComponent(buttons[i]);

            int finalI = i;
            directionalLayout.setClickedListener(component -> {
                type_text.setText(buttons[finalI].getText());
                images[selectButtonIndex].setAlpha((float) 0.2);
                buttons[selectButtonIndex].setAlpha((float) 0.2);
                images[finalI].setAlpha(1);
                buttons[finalI].setAlpha(1);
                selectButtonIndex = finalI;
            });

            select_project.addComponent(directionalLayout);
        }

    }

    void makeToast(String msg) {
        ToastDialog td = new ToastDialog(this);
        td.setText(msg);
        td.setAlignment(LayoutAlignment.CENTER);
        td.setDuration(1000);
        td.show();
    }

    private void initTextField() {
        data_date_field = (Text) findComponentById(ResourceTable.Id_data_date);
        info_field = (TextField) findComponentById(ResourceTable.Id_data_info);
        money_field = (TextField) findComponentById(ResourceTable.Id_data_money);
        account_field = (TextField) findComponentById(ResourceTable.Id_data_account);
        data_date_field.setText("");
        info_field.setText("");
        money_field.setText("");
        account_field.setText("");
        data_date_field.setClickedListener(component -> AddAccountSlice.this.showContactDetailDialog());
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
